package com.marvels.financial.loan.api.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.DateUtils;
import com.marvels.financial.consum.FinancialConsumerLoanApplyApi;
import com.marvels.financial.loan.api.FinancialCheckBillApi;
import com.marvels.financial.loan.dao.FinancialCheckBillDao;
import com.marvels.financial.loan.dto.CheckBillDTO;
import com.marvels.financial.loan.dto.EverydayStatisticsDTO;

@Service
public class FinancialCheckBillImpl implements FinancialCheckBillApi{
	
	@Autowired
	private FinancialCheckBillDao checkBillDao;

	@Override
	public List<EverydayStatisticsDTO> selectEverydayStatistics(CheckBillDTO dto) throws MarvelsBusinessException{
		if(StringUtils.isEmpty(dto.getStartDate())){//开始时间为空
			throw new MarvelsBusinessException("请选择开始日期");
		}
		if(StringUtils.isEmpty(dto.getEndDate())){
			throw new MarvelsBusinessException("请选择结束日期");
		}
		PageHelper.startPage(dto.getPageNo(), dto.getPageSize());
		return checkBillDao.newSelectDailyStatistics(dto);
	}
	
	public List<EverydayStatisticsDTO> repairEverydayStatisticsData(List<EverydayStatisticsDTO> list,CheckBillDTO dto){
		DecimalFormat df=new DecimalFormat("0.00");
		List<EverydayStatisticsDTO> tempList = new ArrayList<EverydayStatisticsDTO>();
		for (int i = 0,j=list.size(); i < j; i++) {
			if(i==0){
//				//统计总数
//				String startDate = dto.getStartDate();
//				String endDate = dto.getEndDate();
//				
//				if(StringUtils.isBlank(startDate)){
//					startDate = DateUtils.getDateYYYY_MM_DD(new Date());
//				}else{
//					startDate = startDate.substring(0,10);
//				}
//				startDate += " 00:00:00";
//				if(StringUtils.isBlank(endDate)){
//					endDate = DateUtils.getDateYYYY_MM_DD(new Date());
//				}else{
//					endDate = endDate.substring(0,10);
//				}
//				endDate += " 23:59:59";
				String startDate = DateUtils.getDateYYYY_MM_DD(DateUtils.getDateYYYY_MM_DD(dto.getStartDate()));
				String endDate = DateUtils.getDateYYYY_MM_DD(DateUtils.getDateYYYY_MM_DD(dto.getEndDate()));
				CheckBillDTO checkBillDTO=new CheckBillDTO();
				checkBillDTO.setStartDate(startDate);
				checkBillDTO.setEndDate(endDate);
				//获取总数统计值
				EverydayStatisticsDTO totleDto1 = checkBillDao.selectStatistics(checkBillDTO);
				
				//获取时间段内的贷款总金额
				Double totleLoanAmount = checkBillDao.countLoanPrincipalAmtAct(startDate, endDate);
				
				//分子：逾期本金 - 展期金额 - 收款滞纳金
				BigDecimal overdueLoanAmount = totleDto1.getDelqDaysMoneyCount().subtract(totleDto1.getZhanqiMoneyCount()).subtract(totleDto1.getPenaltyFee2Count());
				
				if (overdueLoanAmount!=null && totleLoanAmount!=null && totleLoanAmount.doubleValue()>0) {
					double returnSuccessRate = (double)overdueLoanAmount.doubleValue()/totleLoanAmount*100;
					totleDto1.setDelqDaysMoneyRate(df.format( returnSuccessRate)+"%");
				}else{
					totleDto1.setDelqDaysMoneyRate("0.00%");
				}
				totleDto1.setDays("总计");
				tempList.add(totleDto1);
			}
			//统计逾期百分比
			EverydayStatisticsDTO fDto = list.get(i);
			
			if(fDto.getLoanCount()==null || fDto.getLoanCount()==0){
				fDto.setDelqDaysMoneyRate("0.00%");
			}else if(fDto.getDelqDaysCount()==null || fDto.getDelqDaysCount()==0){
				fDto.setDelqDaysMoneyRate("0.00%");
			}else if(fDto.getDelqDaysCount()!=null && fDto.getDelqDaysCount()>0){
				//获取当日贷款总金额
				Double totleLoanAmount = checkBillDao.selectLoanAmount(fDto.getDays());
				//分子：逾期本金 - 展期金额 - 收款滞纳金
				BigDecimal overdueLoanAmount = fDto.getDelqDaysMoneyCount().subtract(fDto.getZhanqiMoneyCount()).subtract(fDto.getPenaltyFee2Count());
				
				if (overdueLoanAmount!=null && totleLoanAmount!=null && totleLoanAmount.doubleValue()>0) {
					double returnSuccessRate = (double)overdueLoanAmount.doubleValue()/totleLoanAmount*100;
					fDto.setDelqDaysMoneyRate(df.format( returnSuccessRate)+"%");
				}else{
					fDto.setDelqDaysMoneyRate("0.00%");
				}
			}
			tempList.add(fDto);
		}
		return tempList;
	}
	

	public HSSFWorkbook findEverydayStatisticsExcelWorkbook(CheckBillDTO dto) throws MarvelsBusinessException{
		HSSFWorkbook wb = new HSSFWorkbook();
		List<EverydayStatisticsDTO> reminderList = selectEverydayStatistics(dto);
		HSSFSheet sheet = wb.createSheet("流量统计");
		HSSFRow row = sheet.createRow(0);
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
		HSSFCell cell = row.createCell((short) 0);
		cell.setCellValue("日期");
		cell.setCellStyle(style);
		cell = row.createCell((short) 1);
		cell.setCellValue("放款单数");
		cell.setCellStyle(style);
		cell = row.createCell((short) 2);
		cell.setCellValue("放款金额");
		cell.setCellStyle(style);
		cell = row.createCell((short) 3);
		cell.setCellValue("放款手续费");
		cell.setCellStyle(style);
		cell = row.createCell((short) 4);
		cell.setCellValue("续借单数");
		cell.setCellStyle(style);
		cell = row.createCell((short) 5);
		cell.setCellValue("续借金额");
		cell.setCellStyle(style);
		cell = row.createCell((short) 6);
		cell.setCellValue("续借手续费");
		cell.setCellStyle(style);
		cell = row.createCell((short) 7);
		cell.setCellValue("收款滞纳金");
		cell.setCellStyle(style);
		cell = row.createCell((short) 8);
		cell.setCellValue("收款本金");
		cell.setCellStyle(style);
		cell = row.createCell((short) 9);
		cell.setCellValue("逾期单数");
		cell.setCellStyle(style);
		cell = row.createCell((short) 10);
		cell.setCellValue("逾期金额");
		cell.setCellStyle(style);
		cell = row.createCell((short) 11);
		cell.setCellValue("展期金额");
		cell.setCellStyle(style);
		cell = row.createCell((short) 12);
		cell.setCellValue("逾期百分比");
		cell.setCellStyle(style);
		//写入实体数据 实际应用中这些数据从数据库得到，
		for (int i = 0; i < reminderList.size(); i++) {
			EverydayStatisticsDTO data = reminderList.get(i);
			row = sheet.createRow((int) i + 1);
			// 创建单元格，并设置值
			cell = row.createCell((short) 0);
			cell.setCellValue(data.getDays());
			cell = row.createCell((short) 1);
			
			cell.setCellValue((data.getLoanCount()==null?0:data.getLoanCount()));						
		    cell = row.createCell((short) 2);
			cell.setCellValue((data.getLoanMoneyCount()==null?"0":data.getLoanMoneyCount().toString()));
			cell = row.createCell((short) 3);
			cell.setCellValue((data.getLoanServiceFeeCount()==null?"0":data.getLoanServiceFeeCount().toString()));
			
			cell = row.createCell((short) 4);
			cell.setCellValue((data.getContinueCout()==null?"0":data.getContinueCout().toString()));
			cell = row.createCell((short) 5);
			cell.setCellValue((data.getContinueMoneyCount()==null?"0":data.getContinueMoneyCount().toString()));
			cell = row.createCell((short) 6);
			cell.setCellValue((data.getContinueServiceFeeCount()==null?"0":data.getContinueServiceFeeCount().toString()));
			cell = row.createCell((short) 7);
			cell.setCellValue((data.getPenaltyFee2Count()==null?"0":data.getPenaltyFee2Count().toString()));						

			cell = row.createCell((short) 8);
			cell.setCellValue((data.getPrincipalActCount()==null?"0":data.getPrincipalActCount().toString()));				

			cell = row.createCell((short) 9);
			cell.setCellValue((data.getDelqDaysCount()==null?"0":data.getDelqDaysCount().toString()));			
			
			cell = row.createCell((short) 10);
			cell.setCellValue((data.getDelqDaysMoneyCount()==null?"0":data.getDelqDaysMoneyCount().toString()));				

			cell = row.createCell((short) 11);
			cell.setCellValue((data.getZhanqiMoneyCount()==null?"0":data.getZhanqiMoneyCount().toString()));				

			cell = row.createCell((short) 12);
			cell.setCellValue((data.getDelqDaysMoneyRate()==null?"0":data.getDelqDaysMoneyRate().toString()));
		}
		return wb;
	}

	@Override
	public Double selectReturnTotalMoney(String loanIds) {
		if(StringUtils.isEmpty(loanIds)){
			return 0.0;
		}
		List<String> loanIdList = Arrays.asList(loanIds.split(","));
		return checkBillDao.selectReturnTotalMoney(loanIdList);
	}
	
}
